package com.zy.nettypoject.netty.demo;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

public class TestRedis {
    public static void main(String[] args) {
        final byte[] LINE = {13,10};
        Bootstrap bootstrap = new Bootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {

                        ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                //AOF写入方法
                                ByteBuf byteBuf = ByteBufAllocator.DEFAULT.directBuffer();
                                byteBuf.writeBytes("*3".getBytes());
                                byteBuf.writeBytes(LINE);
                                byteBuf.writeBytes("$3".getBytes());
                                byteBuf.writeBytes(LINE);
                                byteBuf.writeBytes("set".getBytes());
                                byteBuf.writeBytes(LINE);
                                byteBuf.writeBytes("$4".getBytes());
                                byteBuf.writeBytes(LINE);
                                byteBuf.writeBytes("name".getBytes());
                                byteBuf.writeBytes(LINE);
                                byteBuf.writeBytes("$5".getBytes());
                                byteBuf.writeBytes(LINE);
                                byteBuf.writeBytes("fuck1".getBytes());
                                byteBuf.writeBytes(LINE);
                                ctx.writeAndFlush(byteBuf);
                            }
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                ByteBuf message =  (ByteBuf)msg;
                                System.out.println(message.toString(Charset.defaultCharset()));
                            }
                        });
                    }
                });
        try {
            ChannelFuture sync = bootstrap.connect(new InetSocketAddress(6379)).sync();
            sync.channel().closeFuture().syncUninterruptibly();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
